from sqlalchemy import Column, BigInteger, String, SmallInteger, Date, DateTime, Index
from app.config.database import Base
from datetime import datetime, date
from pydantic import BaseModel, ConfigDict
from typing import Optional

class Student(Base):
    __tablename__ = "edu_student"
    
    id = Column(BigInteger, primary_key=True, autoincrement=True, comment="学生ID")
    user_id = Column(BigInteger, nullable=False, comment="用户ID")
    student_no = Column(String(20), unique=True, nullable=False, comment="学号")
    student_name = Column(String(50), nullable=False, comment="学生姓名")
    gender = Column(SmallInteger, nullable=False, comment="性别：1-男 2-女")
    birth_date = Column(Date, comment="出生日期")
    id_card = Column(String(20), comment="身份证号")
    phone = Column(String(20), comment="联系电话")
    address = Column(String(200), comment="家庭地址")
    parent_name = Column(String(50), comment="家长姓名")
    parent_phone = Column(String(20), comment="家长电话")
    status = Column(SmallInteger, nullable=False, default=1, comment="状态：0-离校 1-在校")
    create_time = Column(DateTime, default=datetime.utcnow, nullable=False, comment="创建时间")
    update_time = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False, comment="更新时间")
    
    # 索引
    __table_args__ = (
        Index('uk_student_no', 'student_no'),
        Index('idx_student_name', 'student_name'),
    )

class StudentInDB(BaseModel):
    id: int
    user_id: int
    student_no: str
    student_name: str
    gender: int
    birth_date: Optional[date] = None
    id_card: Optional[str] = None
    phone: Optional[str] = None
    address: Optional[str] = None
    parent_name: Optional[str] = None
    parent_phone: Optional[str] = None
    status: int = 1
    create_time: Optional[datetime] = None
    update_time: Optional[datetime] = None

    model_config = ConfigDict(from_attributes=True)